package nettyRPC.proxy;

import nettyRPC.client.ConnectManger;
import nettyRPC.registry.ServiceDiscovry;

import java.lang.reflect.Proxy;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @program: NettyRpc-master
 * @description
 * @author: tkk fendoukaoziji
 * @create: 2019-08-12 18:55
 **/
public class MyRpcClient {
    private String serverAddress;
    private ServiceDiscovry serviceDiscovry;
    private static ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(
            16,16,600L,TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(65536));

    public MyRpcClient(String serverAddress){
        this.serverAddress=serverAddress;
    }

    public MyRpcClient(ServiceDiscovry serviceDiscovry){
        this.serviceDiscovry=serviceDiscovry;
    }

    public static <T> T create(Class<T> interfaceClass){
        return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(),
                new Class<?>[]{interfaceClass},
                new ObjectProxy<T>(interfaceClass)
        );
    }

    public static <T> IAsyncObjectProxy createAssync(Class<T> interfaceClass){
        return new ObjectProxy<T>(interfaceClass);
    }

    public void stop(){
        threadPoolExecutor.shutdown();
        serviceDiscovry.stop();
        ConnectManger.getInstance().stop();
    }

    public static void submit(Runnable task){
        threadPoolExecutor.submit(task);
    }








}
